﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApPropertiesManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.AssignDataFromTC;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xaml;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands;

public class CApPropertiesManager
{
  private List<CUiTCObjectProperty> _selectedPropertiesList;
  private TxWindow _window;
  public static string BOM_LINE_NAME_PROPERTY = "bl_line_name";
  public static string BOM_ITEM_OBJECT_TYPE_PROPERTY = "bl_item_object_type";
  public static string BOM_REVISION_OBJECT_DESCRIPTION_PROPERTY = "bl_rev_object_desc";
  public static string ITEM_REVISION_PLANNING_TYPE = "ItemRevision";
  public static string ME_WORKAREA_REVISION_PLANNING_TYPE = "MEWorkareaRevision";
  public static string STORE_PROPERTY_NAME = nameof (SelectedProperties);

  public CApPropertiesManager(TxWindow window)
  {
    this._window = window;
    string empty = string.Empty;
    object obj;
    TxWindowSettings.RestoreWindowValue(this._window, CApPropertiesManager.STORE_PROPERTY_NAME, ref obj);
    if (obj != null)
      empty = obj.ToString();
    if (!string.IsNullOrEmpty(empty))
    {
      MemoryStream memoryStream = new MemoryStream(Encoding.Default.GetBytes(empty));
      try
      {
        this._selectedPropertiesList = XamlServices.Load((Stream) memoryStream) as List<CUiTCObjectProperty>;
      }
      catch (Exception ex)
      {
        TxApplication.LogWriter.WriteExceptionLine(ex);
      }
    }
    if (this._selectedPropertiesList == null)
    {
      this._selectedPropertiesList = new List<CUiTCObjectProperty>();
      this.AddDefaultProperties(this._selectedPropertiesList);
    }
    else
    {
      List<CUiTCObjectProperty> allProperties = this.GetAllProperties();
      this._selectedPropertiesList.RemoveAll((Predicate<CUiTCObjectProperty>) (x => x.Name != CApPropertiesManager.BOM_LINE_NAME_PROPERTY && allProperties.FirstOrDefault<CUiTCObjectProperty>((Func<CUiTCObjectProperty, bool>) (y => y.Name == x.Name)) == null));
    }
  }

  public IEnumerable<CUiTCObjectProperty> SelectedProperties
  {
    get => (IEnumerable<CUiTCObjectProperty>) this._selectedPropertiesList;
    set
    {
      if (this._selectedPropertiesList.Count > 1)
        this._selectedPropertiesList.RemoveRange(1, this._selectedPropertiesList.Count - 1);
      this._selectedPropertiesList.AddRange(value);
      TxWindowSettings.StoreWindowValue(this._window, CApPropertiesManager.STORE_PROPERTY_NAME, (object) this.GetSelectedPropertiesAsString());
    }
  }

  public IEnumerable<CUiTCObjectProperty> UnselectedProperties
  {
    get
    {
      List<CUiTCObjectProperty> allProperties = this.GetAllProperties();
      allProperties.RemoveAll((Predicate<CUiTCObjectProperty>) (x => this.SelectedProperties.FirstOrDefault<CUiTCObjectProperty>((Func<CUiTCObjectProperty, bool>) (y => y.Name == x.Name)) != null));
      return (IEnumerable<CUiTCObjectProperty>) allProperties;
    }
  }

  private void AddDefaultProperties(List<CUiTCObjectProperty> propertiesList)
  {
    this.AddProperty(propertiesList, CApPropertiesManager.BOM_LINE_NAME_PROPERTY, AssignDataFromTCResources.BOM_LINE_NAME_PROPERTY_NICE_NAME);
    this.AddProperty(propertiesList, CApPropertiesManager.BOM_ITEM_OBJECT_TYPE_PROPERTY, AssignDataFromTCResources.BOM_ITEM_OBJECT_TYPE_PROPERTY_NICE_NAME);
    this.AddProperty(propertiesList, CApPropertiesManager.BOM_REVISION_OBJECT_DESCRIPTION_PROPERTY, AssignDataFromTCResources.BOM_REVISION_OBJECT_DESCRIPTION_PROPERTY_NICE_NAME);
  }

  private void AddProperty(
    List<CUiTCObjectProperty> propertiesList,
    string propertyName,
    string propertyNiceName)
  {
    if (propertiesList.FirstOrDefault<CUiTCObjectProperty>((Func<CUiTCObjectProperty, bool>) (x => x.Name == propertyName)) != null)
      return;
    propertiesList.Add(new CUiTCObjectProperty(propertyName, propertyNiceName));
  }

  private List<CUiTCObjectProperty> GetAllProperties()
  {
    List<CUiTCObjectProperty> allProperties = new List<CUiTCObjectProperty>();
    TxPlanningTypeMetaData typeMetaData1 = CApAssignDataFromTCApp.GetTypeMetaData(CApPropertiesManager.ITEM_REVISION_PLANNING_TYPE);
    TxPlanningTypeMetaData typeMetaData2 = CApAssignDataFromTCApp.GetTypeMetaData(CApPropertiesManager.ME_WORKAREA_REVISION_PLANNING_TYPE);
    if (typeMetaData1 != null && typeMetaData2 != null)
    {
      foreach (TxPlanningTypeFieldData allField in typeMetaData1.AllFields)
      {
        TxPlanningTypeFieldData fieldData = allField;
        if (allProperties.FirstOrDefault<CUiTCObjectProperty>((Func<CUiTCObjectProperty, bool>) (x => x.Name == fieldData.Name)) == null)
          allProperties.Add(new CUiTCObjectProperty(fieldData.Name, fieldData.NiceName));
      }
      foreach (TxPlanningTypeFieldData allField in typeMetaData2.AllFields)
      {
        TxPlanningTypeFieldData fieldData = allField;
        if (allProperties.FirstOrDefault<CUiTCObjectProperty>((Func<CUiTCObjectProperty, bool>) (x => x.Name == fieldData.Name)) == null)
          allProperties.Add(new CUiTCObjectProperty(fieldData.Name, fieldData.NiceName));
      }
    }
    this.AddDefaultProperties(allProperties);
    return allProperties;
  }

  private string GetSelectedPropertiesAsString()
  {
    string empty = string.Empty;
    using (MemoryStream memoryStream = new MemoryStream())
    {
      XamlServices.Save((Stream) memoryStream, (object) this._selectedPropertiesList);
      memoryStream.Position = 0L;
      using (StreamReader streamReader = new StreamReader((Stream) memoryStream))
        return streamReader.ReadToEnd();
    }
  }
}
